#include<stdio.h>
#include<string.h>
#include<algorithm>
#define M 200005
using namespace std;
char s[M];
int n,m,k,a[M],c[M],o[M<<1];
namespace Sub2{
	int f[M<<1];
	void dfs(int x,int d){
		if(x>=(1<<k)) return f[x]=x-(1<<k)+1,void();
		dfs(x<<1,d-1),dfs(x<<1|1,d-1);
		int a=f[x<<1|o[x]],b=f[x<<1|(!o[x])];
		if(::a[a]>=d) f[x]=a;
		else f[x]=b;
	}
	inline void solve(){
		dfs(1,k);
		long long ans=0;
		for(int i=1;i<=m;++i){
			int tk=0;
			while((1<<tk)<c[i]) ++tk;
			ans^=1ll*i*f[1<<(k-tk)];
		}
		printf("%lld\n",ans);
	}
}
int main(){
	freopen("arena.in","r",stdin);
	freopen("arena.out","w",stdout);
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;++i) scanf("%d",a+i);
	for(int i=1;i<=m;++i) scanf("%d",c+i);
	while((1<<k)<n) ++k;
	for(int i=1;i<=k;++i){
		scanf("%s",s);
		for(int j=0;j<(1<<(k-i));++j)
			o[j+(1<<(k-i))]=s[j]^48;
	}
	int T;scanf("%d",&T);
	while(T--){
		int t[4];scanf("%d%d%d%d",t,t+1,t+2,t+3);
		for(int i=1;i<=n;++i) a[i]^=t[i&3];
		Sub2::solve();
		for(int i=1;i<=n;++i) a[i]^=t[i&3];
	}
}